import pandas as pds
import os

#m1.csv表示 一月数据.csv

cvs_File = os.path.join('data','nba','m1.csv') #文件名不能有中文
print(cvs_File)
dataset = pds.read_csv(cvs_File,parse_dates=['Date']) #parse_dates 参数知道哪一列会转换成日期对象显示出来
dataset.columns = ["Date", "Start time", "Visitor Team",
"VisitorPts", "Home Team", "HomePts",'Score type', "OT?", "Notes"]

#print(dataset.ix[:5])

#得到主队胜利情况的数据集,这个运算是pandas内部类实现的得到的是一个<class 'pandas.core.series.Series'>对象
dataset['HomeWin'] = dataset['HomePts'] > dataset['VisitorPts'] 
y_true = dataset['HomeWin'].values
#添加列 设置默认值都是False
dataset['HomeLastWin'] = False
dataset['VisitorLastWin'] = False

from collections import defaultdict

won_last = defaultdict(int)

for index,row in dataset.iterrows():
    home_team = row['Home Team']      
    visitor_team = row['Visitor Team']    
    row['HomeLastWin'] = won_last[home_team]    
    row['VisitorLastWin'] = won_last[visitor_team]
     
    dataset.ix[index] = row
    won_last[home_team] = row['HomeWin']
    won_last[visitor_team] = not row['HomeWin']

#print(dataset.ix[20:25])

#使用决策树
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score
import numpy as np

clf = DecisionTreeClassifier(random_state=14)
X_previouswins = dataset[['HomeLastWin','VisitorLastWin']].values
print(X_previouswins.shape)
#交叉验证数据
scores = cross_val_score(clf,X_previouswins,y_true,scoring="accuracy")
accuracy = np.mean(scores)
print(accuracy*100)





